<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

if($komaConfig->getVar('IsOffline') && $komaUserHandler->getVar('rid') != KOMA_ROLE_ADMIN) show_message(KOMA_URL, T_('This site is temporary unvailable due to maintenance. Please come back later.'));

define('KOMA_BLOCK_LEFT', 0);
define('KOMA_BLOCK_RIGHT', 1);
define('KOMA_BLOCK_HEADER', 2);
define('KOMA_BLOCK_FOOTER', 3);
define('KOMA_BLOCK_CONTENT', 4);
		
define('KOMA_BLOCK_INVISIBLE',0);
define('KOMA_BLOCK_VISIBLE',1);

ob_start();

if(!defined('KOMA_CACHE_PATH'))
{
    throw new Exception('Undefined KOMA_CACHE_PATH');
}

$komaTpl = KomaTemplate::getInstance();
$komaPage = KomaPage::getInstance();

$meta = array();
$meta['keyword'] = $komaConfig->getVar('MetaKey');
$meta['description'] = $komaConfig->getVar('MetaDescription');
$meta['author'] = $komaConfig->getVar('MetaAuthor');
$meta['copyright'] = $komaConfig->getVar('MetaCopy');
$meta['generator'] = 'KOMA';

$komaTpl->assign('time', '');
$komaTpl->assign('url', '');

$komaTpl->assign('meta', $meta);
$komaTpl->assign('favicon', KOMA_DEFAULT_FAVICON);
$komaTpl->assign('title', KOMA_SITE_TITLE.' - '.KOMA_SITE_SLOGAN);

$komaTpl->assign('koma_themes_url', KOMA_URL.'/themes/'.KOMA_THEME);
$komaTpl->assign('koma_themes_image_url', KOMA_URL.'/themes/'.KOMA_THEME.'/images');

$komaTpl->assign('block_left', array());
$komaTpl->assign('block_right', array());
$komaTpl->assign('block_header', array());
$komaTpl->assign('block_footer', array());
$komaTpl->assign('block_content', array());

$komaPage->Title = KOMA_SITE_TITLE.' - '.KOMA_SITE_SLOGAN;

if(!isset($contents)) $contents = '';

$component = null;

if(file_exists('./args.php'))
{
    include('./args.php');

    if(!isset($args['StartupClass'])) throw new KomaStartupClassException(array('code' => NOT_DEFINED));
    $komaRoutines->includeOnce('./classes/'.$args['StartupClass'].'.class.php');
    
    if(!class_exists($args['StartupClass'])) throw new KomaStartupClassException(array('code' => NOT_FOUND, 'name' => $args['StartupClass']));
    if(!is_subclass_of($args['StartupClass'], 'KomaComponent')) throw new KomaSubclassException($args['StartupClass'], 'KomaComponent');
    
    $component = new $args['StartupClass'];
}
else if(file_exists('./../args.php'))
{
    include('./../args.php');
    
    if(substr(dirname($_SERVER['PHP_SELF']), -5, 5) == 'admin')
    {
	if(!isset($args['AdminStartupClass'])) throw new KomaStartupClassException(array('code' => NOT_DEFINED));
	//$komaRoutines->includeOnce('./classes/'.$args['AdminStartupClass'].'.class.php');
	$komaRoutines->includeOnce('./../classes/admin/'.$args['AdminStartupClass'].'.class.php');
	
	if(!class_exists($args['AdminStartupClass'])) throw new KomaStartupClassException(array('code' => NOT_FOUND, 'name' => $args['AdminStartupClass']));
	if(!is_subclass_of($args['AdminStartupClass'], 'KomaComponentAdmin')) throw new KomaSubclassException($args['AdminStartupClass'], 'KomaComponentAdmin');
	
	$component = new $args['AdminStartupClass'];
    }
}

if($component != null)
{
    ob_start();
    
    $component->initialize(array('dir' => $args['ComponentDir'], 'version' => $args['Version']['Major'].'.'.$args['Version']['Minor']));
    $component->main();
    $component->finalize();

    $contents = ob_get_clean();
    if($component->Render == false) exit($contents);
    
}

$komaTpl->assign('title', $komaPage->Title);

$result = $komaDB->query(sprintf("SELECT `r`.`rid`, `r`.`bid`, `c`.`dirname`, `b`.`side`, `b`.`class`
                 FROM `%s` `r`, `%s` `b`, `%s` `c` WHERE `r`.`rid` = '%s' AND `b`.`bid` = `r`.`bid` AND `c`.`cid` = `b`.`cid` AND b.visibility = '%s'",
                 $komaDB->prefix('role_permission'), $komaDB->prefix('blocks'), $komaDB->prefix('components'), $komaUserHandler->getVar('rid'), KOMA_BLOCK_VISIBLE));

while($row = $komaDB->fetchAssoc($result))
{   
    gettext_init(KOMA_LOCALE, $row['dirname'].'.blocks', KOMA_ROOT_PATH.'/components/'.$row['dirname'].'/locales', KOMA_CHARSET);

    $show = false;
    $block = null;
    
    if(file_exists(KOMA_ROOT_PATH.'/components/'.$row['dirname'].'/classes/blocks/'.$row['class'].'.class.php'))
    {
	include_once(KOMA_ROOT_PATH.'/components/'.$row['dirname'].'/classes/blocks/'.$row['class'].'.class.php');
	
        $block = new $row['class'];
        $block->initialize(array('dirname' => $row['dirname']));
	$show = $block->main();
	$block->finalize();
    }

    if($show)
    {
        $fetched = array('title' => $block->Title, 'content' => $block->Content);
        
	switch($row['side'])
	{
	    case KOMA_BLOCK_LEFT:
                $komaTpl->append('block_left', $fetched);
		break;
	    case KOMA_BLOCK_RIGHT:
		$komaTpl->append('block_right', $fetched);
		break;
	    case KOMA_BLOCK_HEADER:
		$komaTpl->append('block_header', $fetched);
		break;
	    case KOMA_BLOCK_FOOTER:
		$komaTpl->append('block_footer', $fetched);
		break;
	    case KOMA_BLOCK_CONTENT:
		$komaTpl->append('block_content', $fetched);
		break;
	}
	
	unset($show);
    }
}

KomaStyleManager::getInstance()->addSource(KOMA_ROOT_PATH.'/themes/'.KOMA_THEME.'/theme.css');

$_SESSION['JSSources'] = KomaScriptManager::getInstance()->getSources();
$_SESSION['JSScripts'] = KomaScriptManager::getInstance()->getScripts();

$_SESSION['CSSSources'] = KomaStyleManager::getInstance()->getSources();
$_SESSION['CSSStyles'] = KomaStyleManager::getInstance()->getStyles();

$komaTpl->assign('scriptmanager', '<script type="text/javascript" src="'.KOMA_URL.'/jsaggregator.php/'.md5(serialize(KomaScriptManager::getInstance())).'"></script>');
$komaTpl->assign('stylemanager', '<link rel="stylesheet" type="text/css" href="'.KOMA_URL.'/cssaggregator.php/'.md5(serialize(KomaStyleManager::getInstance())).'"></link>');

$komaTpl->assign('contents', $contents);
$komaTpl->display(KOMA_ROOT_PATH.'/themes/'.KOMA_THEME.'/presentation.html');
?>